package com.lam.myview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

import com.lam.fundamental.R;


public class CompassView extends View{

	private float bearing ;  //show the direction
	private Paint markerPaint;
	private Paint textPaint;
	private Paint circlePaint;
	private String northString;
	private String westString;
	private String eastString;
	private String southString;
	private int textHeight;
	
	public float getBearing() {
		return bearing;
	}

	public void setBearing(float bearing) {
		this.bearing = bearing;
		sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
	}

	public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		initCompassView();
	}

	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initCompassView();
	}

	public CompassView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initCompassView();
	}
	
	@Override
	public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
		// TODO Auto-generated method stub
		return super.dispatchPopulateAccessibilityEvent(event);
//		if(isShown()){
//			String bearingStr=String.valueOf(bearing);
//			if(bearingStr.length()>AccessibilityEvent.MAX_TEXT_LENGTH){
//				bearingStr=bearingStr.substring(0,AccessibilityEvent.MAX_TEXT_LENGTH);
//				event.getText().add(bearingStr);
//				return true;
//			}else {
//				return false;
//			}
//		}
	}

	protected void initCompassView(){
		setFocusable(true);
		
		Resources resources= this.getResources();
		
		circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(resources.getColor(R.color.compass_bg));
		circlePaint.setStrokeWidth(1);
		circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		
		northString=resources.getString(R.string.cardinal_north);
		eastString=resources.getString(R.string.cardinal_east);
		westString=resources.getString(R.string.cardinal_west);
		southString=getResources().getString(R.string.cardinal_south);
		
		textPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(resources.getColor(R.color.compass_text));
		
		textHeight=(int)textPaint.measureText("yY");
		markerPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		markerPaint.setColor(resources.getColor(R.color.compass_marker));
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		int measureWidth=getMeasuredWidth();
		int measureHeight=getMeasuredHeight();
		
		int px=measureWidth/2;
		int py=measureHeight/2;
		
		int radius =Math.min(px, py);
		
		canvas.drawCircle(px, py, radius, circlePaint);
		
		canvas.save();
		canvas.rotate(-bearing,px,py);
		
		int textWidth =(int)textPaint.measureText("W");
		int cardinalX=px-textWidth/2;
		int cardinalY=py-radius+textHeight;
		for(int i=0;i<24;i++){
			canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
			canvas.save();
			canvas.translate(0, textHeight);
			
			if(i%6==0){
				String dirString="";
				switch (i) {
				case 0:
					dirString=northString;
					int arrowY=2*textHeight;
					canvas.drawLine(px, arrowY, px-5, 3*textHeight,markerPaint);
					canvas.drawLine(px, arrowY, px+5, 3*textHeight,markerPaint);
					break;
				case 6:dirString=eastString;break;
				case 12:dirString=southString;break;
				case 18:dirString=westString;break;
				default:
					break;
				}
				canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
			}
			
			else if(i%3==0){
				String angle=String.valueOf(i*15);
				float angleTextWidth=textPaint.measureText(angle);
				int angleTextX=(int)(px-angleTextWidth/2);
				int angleTextY=py-radius+textHeight;
				canvas.drawText(angle, angleTextX, angleTextY, textPaint);
			}
			canvas.restore();
			canvas.rotate(15,px,py);
		}
		canvas.restore();
		super.onDraw(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int measureWidth=measure(widthMeasureSpec);
		int measureHeight=measure(heightMeasureSpec);
		int d =Math.min(measureHeight, measureWidth);
		setMeasuredDimension(d, d);
	}

	private int measure(int measureSpec){
		int result=0;
		int specMode=MeasureSpec.getMode(measureSpec);
		int specSize=MeasureSpec.getSize(measureSpec);
		if(specMode==MeasureSpec.UNSPECIFIED){
			result=200; //default 
		}else{
			result=specSize;
		}
		return result;
	}
	
	
}
